package edu.java.lectures.lec01.main;

/**
 * Represents point from the 2D plain.
 * 
 * @author Nikolay Vasilev
 */
public class Point2D {

    // --- Instance Variables --------------------------------------------------

    private double x = 4; // if no value assigned -> the default value is put
    private double y = 1;

    // --- Constructors --------------------------------------------------------

    public Point2D() { // NOT DEFAULT
	this.x = 0;
	this.y = 0;
    }

    public Point2D(double x, double y) {
	// System.out.println("[" + this.x + " , " + this.y + "]");
	this.x = x;
	this.y = y;
	// System.out.println("[" + this.x + " , " + this.y + "]");
    }

    // --- Setters and Getters -------------------------------------------------

    public double getX() {
	return x;
    }

    public void setX(double x) {
	if (x < 0) {
	    System.out.println("Don't tease the master!");
	    this.x = 0;
	    return;
	}
	this.x = x;
    }

    public double getY() {
	return y;
    }

    public void setY(double y) {
	this.y = y;
    }

    // --- Methods -------------------------------------------------------------

    /**
     * Returns representation of the poing in the format of: (x,y)
     * 
     * @return Returns representation of the poing in the format of: (x,y)
     */
    @Override
    public String toString() {
	String result = "(" + x + " , " + y + ")";
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	// return super.equals(obj); // the default behaviour
	if (obj == null) {
	    return false;
	}
	if (this == obj) { // optimization from String.equals(Object)
	    return true;
	}
	if (!(obj instanceof Point2D)) {
	    return false;
	}
	Point2D otherPoint = (Point2D) obj;
	double otherX = otherPoint.getX();
	double otherY = otherPoint.getY();
	boolean result = (this.x == otherX) && (this.y == otherY);

	// Other (lazy) way to compare objects:
	// Point otherPoint = (Point) obj;
	// String myStr = this.toString();
	// String otherStr = otherPoint.toString();
	// boolean result = myStr.equals(otherStr);

	return result;
    }

    public static void main(String[] args) {
	Point2D p = new Point2D(3, 4);
	double pX = p.x; // why it works ->
	// Answer: private works ONLY in the CLASS => because main is in the
	// same class you've got access to the field p.x

	System.out.println(pX);
    }
}
